// pages/goods/selectGoods.js
import { goodsCategroyList, goodsList } from "../../api/goodsCategroy";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        focus: false,
        keyWord: '',

        pageNum:1,
        pageSize:20,

        category: [],
        category_id: null,
        categoryActiv: 0,
        goods: [],
        selectName: '',
        goodsId: null,
        goodsActiv: null,

    },

    keyword(e) {
        this.setData({
            keyWord: e.detail.value
        })
    },
    focus() {
        this.setData({
            focus: true
        })
    },
    blur() {
        this.setData({
            focus: false
        })
    },
    search() {
        goodsList({keyword:this.data.keyWord,direction: 'asc'}).then(res => {
            console.log(res,'2222');
        })
    },

    getGoods() {
        goodsCategroyList({ direction: 'asc' }).then(res => {
            console.log(res, 'goodsCategroyList');
            this.setData({
                category: res.data.data
            })
        })
        goodsList({ direction: 'asc' }).then(res => {
            console.log(res, 'goodsList');

            this.setData({
                goods: res.data.data
            })
        })
    },

    tapProduct(e) {
        let goodsCategoryId;

        if (e.currentTarget.dataset.category_id == 0) {
            goodsCategoryId = null
        } else {
            goodsCategoryId = e.currentTarget.dataset.category_id
        }
        goodsList({
            goodsCategoryId: goodsCategoryId,
            direction: 'asc'
        }).then(res => {
            console.log(res, 'goodsList');

            this.setData({
                goods: res.data.data
            })
        })
        this.setData({
            categoryActiv: e.currentTarget.dataset.index,
            category_id: e.currentTarget.dataset.category_id
        })
        console.log(this.data.category_id);

    },

    tapProductItem(e) {
        this.setData({
            goodsId: e.currentTarget.dataset.goods_id,
            selectName: e.currentTarget.dataset.name,
            goodsActiv: e.currentTarget.dataset.index
        })
        //给上一个页面传参
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
            goodsId: e.currentTarget.dataset.goods_id,
            goodsName: e.currentTarget.dataset.name,
            goodsSn:e.currentTarget.dataset.sn
        })
        wx.navigateBack({
            delta: 1
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getGoods()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
})